package mvc;

import java.io.IOException;
import java.io.PrintWriter;
import java.util.Map;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import mvc.unti.FullBean;
import mvc.action.LoginAction;
import mvc.action.RegisterAction;
import mvc.form.ActionForm;
import mvc.form.LoginForm;
import mvc.form.RegisterForm;
import mvc.form.Action;

/**
 * Servlet implementation class ActionServlet
 */
public class ActionServlet extends HttpServlet {
	private static final long	serialVersionUID	= 1L;

	/**
	 * @see HttpServlet#HttpServlet()
	 */
	public ActionServlet() {
		super();
		// TODO Auto-generated constructor stub
	}

	/**
	 * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse
	 *      response)
	 */
	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		// TODO Auto-generated method stub
		//String sign = request.getParameter("sign");
//		LoginForm form = (LoginForm)FullBean.full(request);
//		System.out.println(form);
//		ActionForm form = FullBean.full(request);
//		System.err.println(form);
		request.setCharacterEncoding("UTF-8");
		response.setCharacterEncoding("UTF-8");
		ActionForm actionForm = FullBean.full(request);
		Action action = null;
		Map<String, String> map = ActionMapping.getMap();
		try{
			String actionName = map.get(request.getParameter("sign"));
			Class clazz = Class.forName(actionName);
			action = (Action)clazz.newInstance();
		}catch(Exception e){
			e.printStackTrace();
		}
//		if(request.getParameter("sign").equals("mvc.form.LoginForm")){
//			action = new LoginAction();
//		}
//		if(request.getParameter("sign").equals("mvc.form.RegisterForm")){
//			action = new RegisterAction();
//		}
		String message = action.execute(actionForm);
		PrintWriter out = response.getWriter();
		out.print(message);
		out.flush();
		out.close();
	}

	/**
	 * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse
	 *      response)
	 */
	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException,
			IOException {
		// TODO Auto-generated method stub
		this.doGet(request, response);
	}

}
